package services

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	"hft/Http/Models/request"
	"hft/Http/Services/base"
	utils "hft/Http/Utils"
	"hft/global"
	"hft/tools"
	"time"

	"gorm.io/gorm"
)

type Active struct{}

func (*Active) CreateActive(claims tools.BaseClaims, article *request.ActiveReq) (err error) {

	var account *admin.Account

	var group *admin.ActiveGroup

	var loupan *admin.LouPan

	if article.LouPanId == 0 {
		return errors.New("无效的楼盘信息")
	}

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("无该账号信息")
	}

	err = global.OC_DB.First(&group, article.ActiveGroup).Error

	if err != nil {
		return errors.New("分组不存在")
	}

	err = global.OC_DB.First(&loupan, article.LouPanId).Error

	if err != nil {
		return errors.New("你无权在该楼盘添加动态")
	}

	// if group.AccountId != account.ID {
	// 	return errors.New("你不可能在此分组中添加或修改动态")
	// }

	// err = global.OC_DB.Create().Error

	err = global.OC_DB.Transaction(func(tx *gorm.DB) error {

		err = tx.Create(&admin.ActiveBase{
			AccountId:   int(account.ID),
			AccountLink: fmt.Sprintf("%v%v,", account.AccountLink, account.ID),
			LouPanId:    article.LouPanId,
			HasImg:      article.HasImg,
			HasVideo:    article.HasVideo,
			Name:        article.Name,
			Cover:       article.Cover,
			ActiveGroup: article.ActiveGroup,
			Cont:        &admin.ActiveCont{Content: article.Cont},
		}).Error

		if err != nil {
			return errors.New("未知错误500")
		}
		fmt.Println(time.Now(), time.Now().UnixNano()/int64(time.Millisecond))
		err = tx.Model(&admin.LouPan{}).Where("id = ?", article.LouPanId).Update("active_last_update", time.Now().Unix()).Error

		return err
	})

	if err != nil {
		return errors.New("未知错误500")
	}

	return nil
}

func (*Active) EditActive(claims tools.BaseClaims, article *request.ActiveReq) (err error) {
	var account *admin.Account

	var group *admin.ActiveGroup

	var loupan *admin.LouPan

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("无该账号信息")
	}

	err = global.OC_DB.First(&group, article.ActiveGroup).Error

	if err != nil {
		return errors.New("分组不存在")
	}

	err = global.OC_DB.First(&loupan, article.LouPanId).Error

	if err != nil {
		return errors.New("你无权在该楼盘添加动态")
	}

	// if group.AccountId != account.ID {
	// 	return errors.New("你不可能在此分组中添加或修改动态")
	// }
	err = global.OC_DB.Model(&admin.ActiveBase{}).Where("id = ?", article.ID).Updates(&admin.ActiveBase{
		HasImg:      article.HasImg,
		HasVideo:    article.HasVideo,
		Name:        article.Name,
		Cover:       article.Cover,
		ActiveGroup: article.ActiveGroup,
		RegionLink:  account.RegionLink,
	}).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	err = global.OC_DB.Model(&admin.ActiveCont{}).Where("id = ?", article.ID).Updates(&admin.ActiveCont{Content: article.Cont}).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	return nil
}

func (*Active) GetList(claims tools.BaseClaims, paginate *request.ActiveSearchReq) (data *utils.Page, err error) {

	var articles []*admin.ActiveBase

	if paginate.LouPanId == 0 {
		return nil, errors.New("无效的楼盘信息")
	}

	account, err := base.FindAccount(claims, global.OC_DB)

	if err != nil {
		return nil, err
	}

	global.OC_DB.Scopes(dao.GetActiveDao.GetActiveList(account.RegionLink, paginate, global.OC_DB)).Order("create_time desc").Find(&articles).Offset(-1).Limit(-1).Count(&paginate.Total)

	for _, v := range articles {
		v.Cover = global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover
	}

	paginate.Data = articles

	return &paginate.Page, nil
}

func (*Active) GetActive(claims tools.BaseClaims, id int) (article *admin.ActiveBase, err error) {

	var account *admin.Account

	var search *admin.ActiveBase

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("无该账号信息")
	}

	err = global.OC_DB.Preload("Cont", func(db *gorm.DB) *gorm.DB {
		return db.Omit("id,update_time,create_time")
	}).First(&search, id).Error

	if err != nil {
		return nil, errors.New("找不到动态信息")
	}

	return search, nil
}

func (*Active) DeleteAct(ids []int, claims tools.BaseClaims) (err error) {

	var account *admin.Account

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("该账号信息不存在")
	}

	var actList []*admin.ActiveBase

	global.OC_DB.Find(&actList, ids)

	res := utils.IsThisActBelongThisAccount(actList, account.AccountLink)

	if !res {
		return errors.New("你无权删除该动态")
	}

	global.OC_DB.Model(&admin.ActiveBase{}).Unscoped().Delete(&actList)

	return nil

}

var ActiveService = new(Active)
